<template>
    <div>
        <h3>添加房型</h3>

        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">房型名称</td>
                    <td>
                        <input type="text" v-model="querinfo.Name">
                    </td>
                </tr>

                <tr>
                    <td align="right">房型图片</td>
                    <td>
                        <input type="file" @change="upimg">
                        <img :src="querinfo.Photo" width="150" height="100">
                    </td>
                </tr>

                <tr>
                    <td align="right">房型价格</td>
                    <td>
                        <input type="text" v-model="querinfo.Price">
                    </td>
                </tr>

                <tr>
                    <td align="right">入住人数</td>
                    <td>
                        <input type="text" v-model="querinfo.Count">
                    </td>
                </tr>
                <tr>
                    <td align="right">是否有窗</td>
                    <td>
                        <input type="radio" name="Iswiodow" :value="1" v-model="querinfo.Iswiodow">是
                        <input type="radio" name="Iswiodow" :value="0" v-model="querinfo.Iswiodow">否
                    </td>
                </tr>

                <tr>
                    <td align="right">房型平米</td>
                    <td>
                        <input type="text" v-model="querinfo.Area">
                    </td>
                </tr>

                <tr>
                    <td align="right">是否发布</td>
                    <td>
                        <input type="radio" name="State" :value="1" v-model="querinfo.State">是
                        <input type="radio" name="State" :value="0" v-model="querinfo.State">否
                    </td>
                </tr>

                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="添加" class="btn btn-primary" @click="ok">
                    </td>
                </tr>

                
            </tbody>
        </table>


    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import moment from 'moment';
import { ref,reactive,onMounted } from 'vue';
import { useRoute,useRouter } from 'vue-router';
const route=useRoute();
const router=useRouter();

let querinfo:any=reactive({
  "Name": "",
  "Photo": "",
  "Price": "",
  "Count": "",
  "Iswiodow": "",
  "Area": "",
  "State": "",
})

//上传图片
const upimg=(e:any)=>{
    let m=e.target.files[0];
    let s=new FormData();
    s.append('file',m);
    axios({
        method:"post",
        url:"/api/House/Upfile",
        data:s
    })
    .then(res=>{
        if(res.data=="图片格式错误"){
            alert('图片格式错误')
        }
        else if(res.data=="大于2MB"){
            alert('图片不能大于2MB')
        }
        else{
            console.log(res);
            alert('上传成功');
            querinfo.Photo=res.data;

        }
    })
}
const ok=()=>{
    axios({
        method:"post",
        url:"/api/House/AddHouseType",
        data:querinfo,
    })
   
    .then(res=>{
        console.log(res)
        if(res.data==-1){
            alert('房型名称重复');
        }
        else if(res.data>0){
            alert('添加成功');
            router.push('/gethouse')
        }
        else{
            alert('添加失败')
        }
    })
    .catch(err=>{
        console.log(err);
    })
}



</script>






<style scoped>

</style>